class QuickUnion(object):
    id = []
    count = 0

    def __init__(self, n):
        self.count = n
        self.id = [x for x in range(n)]

    def connected(self, p, q):
        return self.find(p) == self.find(q)

    def find(self, p):
        while p != self.id[p]:
            p = self.id[p]
        return p

    def union(self, p, q):
        idq = self.find(q)
        idp = self.find(p)
        if not self.connected(p, q):
            self.id[idp] = idq
            self.count -= 1


input = [[1, 1, 0],
         [1, 1, 0],
         [0, 0, 1]]

m = len(input)

qu = QuickUnion(m)
for i in range(m):
    for j in range(m):
        if input[i][j]:
            qu.union(i, j)

print(qu.count)
